/**
 * Created by mapbar_front on 2019/6/6.
 */

import React,{ Component } from 'react';

//词法作用域
//闭包是函数和声明该函数的词法环境的组合。

function createFunctions1(){
    var result = new Array();

    for(var i=0;i<10;i++){
        result[i] = function(num){
            return function(){
                return num;
            }
        }(i)
    }
    return result;
}
console.log(createFunctions1()[1]());

console.log('分界线');
//模块块级作用域
function outputNumbers(count){
    for(var i=0;i<5;i++){
        console.log(i); // 0 1 2 3 4
    }
    var i; //重新声明变量
    console.log(i); //5
}
//outputNumbers(5);

function test1(){
    (function(){
        for(var i=0;i<5;i++){
            console.log(i); // 0 1 2 3 4
        }
    })()
    //有块级作用域
    //console.log(i);
}
test1();

for(let i=0;i<5;i++){
    console.log(i);
}
//let 有块级作用域
//console.log(i);


function test(){
    var num = 1;
    return function(){
        console.log(num);
    }
}
var res = test();
res();


function MyObject(){
    var privateNum = 12;
    function privateFunction(){
        console.log(privateNum);
    }

    //特权方法
    this.publicMethod = function(){
        privateNum++;
        return privateFunction();
    }
}

var myObject = new MyObject();
myObject.publicMethod();

function Person(name){
    this.getName = function(){
        return name;
    }
    this.setName = function(value){
        name = value;
    }
}
var person = new Person('天下');
console.log(person.getName());
person.setName('秦扫六合，统一天下');
console.log(person.getName());
var person2 = new Person('xiao666');
console.log(person2.getName());

(function(){
    var privateNum = 123;
    function privateFunction(){
        console.log(privateNum);
    }

    // function MyObject(){}
    MyObject = function(){}

    MyObject.prototype.publicMethod = function(){
        privateNum++;
        return privateFunction();
    }
})()


(function(){
    var name = '';
    Person = function(value){
        name = value;
    }
    Person.prototype.getName = function(){
        return name;
    }

    Person.prototype.setName = function(value){
        name = value;
    }

})()

var person1 = new Person('tianxia666');
console.log(person1.getName());
person1.setName('xiao666');
console.log(person1.getName());

var person2 = new Person('tianxia');
console.log(person2.getName());
console.log(person1.getName());




function Person(name){
    this.name = name;
}
Person.prototype.sayName = function(){
    return this.name;
}
var person1 = new Person('tianxia');
console.log('person1', person1)

console.log(person1.sayName());
var person2 = new Person('xiao');
console.log('person2', person2)
console.log(person2.sayName());
console.log(person1.sayName());

function BaseComponent(){

}
var application = function(){
    var components = new Array();

    //初始化
    components.push(new BaseComponent());

    //公共
    return {
        getComponentCount: function(){
            return components.length;
        },
        registerComponent: function(component){
            if(typeof component == "object") {
                components.push(component);
            }
        }
    }
}()


//7.4 私有变量
//我们把有权访问私有变量和私有函数的方法称为特权方法。
function MyObject(){
    //私有变量和私有函数
    var privateVariable = 10;

    function privateFunction(){
        return privateVariable;
    }
    //特权方法

    this.publicMethod = function(){
        privateVariable++;
        return privateFunction();
    }
}
var person1 = new MyObject();
var res = person1.publicMethod();
console.log(res);

function Person(name){
    this.getName = function(){
        return name;
    }

    this.setName = function(value){
        name = value;
    }
}

var person = new Person('tianxia666');
console.log(person.getName());
person.setName('xiao666');
console.log(person.getName());

var person2 = new Person('秦扫六合');
console.log(person2.getName());
console.log(person.getName());

(function(){
    //私有变量和私有函数
    var privateVariable = 10;

    function privateFunction(){
        return privateVariable;
    }

    //构造函数
    MyObject = function(){

    }
    //特权方法
    MyObject.prototype.publicMethod = function(){
        privateVariable++;
        return privateFunction();
    }
})()

var obj1 = new MyObject();
obj1.publicMethod(); //11


(function(){
    var name = '';
    Person = function(value){
        name = value;
    }
    Person.prototype.getName = function(){
        return name;
    }

    Person.prototype.setName = function(value){
        name = value;
    }

})()

var person1 = new Person('tianxia');
console.log(person1.getName());
person1.setName('tianxia666');
console.log(person1.getName());

var person2 = new Person('xiao');
console.log(person2.getName());
console.log(person1.getName());


var singleton = function(){
    var privateVariable = 10;

    function privateFunction(){
        return privateVariable;
    }

    //特权方法
    return {
        privateVariable: true,
        publicMethod: function(){
            privateVariable++;
            return privateFunction();
        }
    }
}
singleton()

var application = function(){
    var components = new Array('yellow');

    //初始化

    //公共
    return {
        getComponentCount: function(){
            return components.length;
        },
        // registerComponent: function(component){
        //     if(typeof component == 'object'){
        //         components.push(component)
        //     }
        // }
    }
}
application().getComponentCount(); //1


var singleton = function(){
    //定义私有变量和函数
    var privateVariable = 10;

    function privateFunction(){
        return privateVariable;
    }

    return {
        publicProperty: true,
        publicMethod: function(){
            privateVariable++;
            return privateFunction()
        }
    }
}()

var singleton = function(){
    //定义私有变量和函数
    var privateVariable = 10;

    function privateFunction(){
        return privateVariable;
    }

    function CustomType(){}
    //创建对象
    var object = new CustomType()

    object.publicProperty = true;
    object.publicMethod = function(){
        privateVariable++;
        return privateFunction();
    }
    return object;
}()

function test(){
    var n = 10;
    bar = function(){
        console.log(n);
    }
}
test()
bar()

function createFunctions(){
    var result = new Array();

    for(var i=0;i<10;i++){
        result[i] = function(num){
            return function(){
                return num;
            }
        }(i)
    }

    return result;
}
createFunctions();


function fun(n,o){
    console.log(o);
    return {
        fun:function(m){//[2]
            return fun(m,n);//[1]
        }
    }
}

var a=fun(0);
a.fun(1);
a.fun(2);
a.fun(3);
var b=fun(0).fun(1).fun(2).fun(3);
var c=fun(0).fun(1);
c.fun(2);
c.fun(3);

class Closure extends Component{
    render(){
        return(
            <div>123</div>
        )
    }
}
export default Closure;

